function inputDateOnKeyup(e, el, next, back) {
	var length_on = el.value.length;
	var unicode = e.charCode? e.charCode : e.keyCode;
	//document.getElementById('idText').value = 'inputDateOnKeyup :- ' + unicode;
	return false;
}

function inputDateOnKeydown(e, el, next, back) {
	var length_on = el.value.length;
	var unicode = e.charCode? e.charCode : e.keyCode;
	//document.getElementById('idText').value = 'inputDateOnKeydown :- ' + unicode;

	var isShiftKey = false;
	var mozilla = document.getElementById && !document.all;
	if (mozilla) {
		isShiftKey = e.shiftKey;
	} else {
		isShiftKey = event.shiftKey;
	}

	if ((unicode == 9) || ((isShiftKey) && (unicode == 9))) {
		return true;
	}


	if (unicode == 8) {
		if (el.value.length == 0) {
			if (back != '') {
				document.getElementById(back).focus();
				if (mozilla) {
					document.getElementById(back).selectionStart = 2;
				} else {
					if(document.getElementById(back).createTextRange) {
						var range = document.getElementById(back).createTextRange();
						range.move('character', 2);
						range.select();
					}
				}
				return false;
			}
		} else {
			var posS = getCursorPosS(el);
			var posE = getCursorPosE(el);
			//document.getElementById('idText').value = 'position :- ' + posS + " >> " + posE;
			if (posE > posS) {
				el.value = '';
				return false;
			} else {
				var position = getCursorPos(el);
				if (position == 0) {
					if (back != '') {
						document.getElementById(back).focus();
						if (mozilla) {

						} else {
							if(document.getElementById(back).createTextRange) {
								var range = document.getElementById(back).createTextRange();
								range.move('character', 2);
								range.select();
							}
						}
						return false;
					}
				}
			}
		}
	} else if (unicode == 37) {
		var position = getCursorPos(el);
		//document.getElementById('idText').value = 'position :- ' + position;
		if (position == 0) {
			if (back != '') {
				document.getElementById(back).focus();
				if (mozilla) {
					//document.getElementById(back).focus();
					//document.getElementById(back).select();
				} else {
					if(document.getElementById(back).createTextRange) {
						var range = document.getElementById(back).createTextRange();
						range.move('character', 2);
						range.select();
					}
				}
				return false;
			}
		}
	} else if (unicode == 39) {
		var position = getCursorPos(el);
		//document.getElementById('idText').value = 'position :- ' + position;
		if (position == el.value.length) {
			if (next != '') {
				document.getElementById(next).focus();
				if (mozilla) {
					document.getElementById(next).selectionStart = 0;
				}
				//document.getElementById(next).select();
				return false;
			}
		}

	} else {
		var posS = getCursorPosS(el);
		var posE = getCursorPosE(el);
		//document.getElementById('idText').value = 'position :- ' + posS + " >> " + posE;
		if (posE > posS) {
			return true;
		} else {
			if (length_on == el.maxLength ) {
				if (next != '') {
					document.getElementById(next).focus();
					//document.getElementById(next).select();
					return true;
				}
			}
		}
	}
}

function inputDateOnKeypress(e, el, next, back){ // for use
	var unicode = e.charCode? e.charCode : e.keyCode;
	var mozilla = document.getElementById && !document.all;
	if (mozilla){
		if (e.keyCode.toString().search(/^(8|9|45|46|35|36|37|39)$/) != (-1)){
			return true;
		}
	} else {
		if (unicode == 13) {
			return true;
		}
	}

	var numPattern = /^\d*[0-9]?$/;
	var chk = numPattern.test(String.fromCharCode(unicode));
	if (!chk){
		return false;
	}
	return true;
}

function getCursorPosS(ctrl) {
	// get cursor position start hightlight
	var CaretPos = 0;
	if (document.selection) {	// IE Support
		ctrl.focus ();
		if (ctrl.createTextRange) {
			var r = document.selection.createRange().duplicate()
			r.moveEnd('character', ctrl.value.length)
			if (r.text == '') pos = ctrl.value.length
				pos = ctrl.value.lastIndexOf(r.text)
		} else pos = ctrl.selectionStart
		CaretPos= pos
	}// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}
function getCursorPosE(ctrl) {
	// get cursor position end hightlight
	var CaretPos = 0;
	if (document.selection) {	// IE Support
		ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionEnd;
	return (CaretPos);
}

function getCursorPos(ctrl) {
 	var CaretPos = 0;
	// IE Support
	if (document.selection) {
		ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}

function compareDate(start, end) {
	var chk = false;

	var eInt = parseInt(end, 10);
	var sInt = parseInt(start, 10);
	if (end.length == start.length) {
		if ((eInt - sInt) >= 0){
			chk = true;
		}
	}
	return chk;
}

function checkDateValueToSubmit(el_day, el_month, el_year) {
	var chk = "";
	var objday = document.getElementById(el_day);
	var objmonth = document.getElementById(el_month);
	var objyear = document.getElementById(el_year);
	var isYear = false;
	var isMonth = false;
	var isDay = false;

	var y = 0;
	if (objyear.value.length > 0) {
		y = parseInt(objyear.value, 10);
		if ((y >= 2000) && (y <= 3000)){
			isYear = true;
		}
	}

	var m = 0;
	if (objmonth.value.length > 0) {
		m = parseInt(objmonth.value, 10);
		if ((m >= 1) && m <= 12) {
			isMonth = true;
		}
	}

//	D = 29 2012, 2008, 2004 ,2000 ปีที่หาร 4 ลงตัว
//  D = 31 1, 3, 5, 7, 8, 10, 12
//  D = 30 4, 6, 9, 11
	var d = 0;
	if (isMonth == true) {
		d = parseInt(objday.value, 10);
		if ((m == 4) || (m == 6) || (m == 9) || (m == 11)) {
			if ((d >= 1) && d <= 30) {
				isDay = true;
			}
		} else if ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12)) {
			if ((d >= 1) && d <= 31) {
				isDay = true;
			}
		} else {
			if (isYear == true) {
				if ((y % 4) == 0) {
					if ((d >= 1) && d <= 29) {
						isDay = true;
					}
				} else {
					if ((d >= 1) && d <= 28) {
						isDay = true;
					}
				}
			}
		}
	}

	if (isYear == false) {
		chk = 'not year';
	} else if (isMonth == false) {
		chk = 'not month';
	} else if (isDay == false) {
		chk = 'not day';
	} else {
		chk = 'ok';
	}
	return chk;
}

function setDateValueToPicker(el_day, el_month, el_year, el) {

	var objday = document.getElementById(el_day);
	var objmonth = document.getElementById(el_month);
	var objyear = document.getElementById(el_year);
	var isYear = false;
	var isMonth = false;
	var isDay = false;

	var y = 0;
	if (objyear.value.length > 0) {
		y = parseInt(objyear.value, 10);
		if ((y >= 2000) && (y <= 3000)){
			isYear = true;
		}
	}

	var m = 0;
	if (objmonth.value.length > 0) {
		m = parseInt(objmonth.value, 10);
		if ((m >= 1) && m <= 12) {
			isMonth = true;
		}
	}

//	D = 29 2012, 2008, 2004 ,2000 ปีที่หาร 4 ลงตัว
//  D = 31 1, 3, 5, 7, 8, 10, 12
//  D = 30 4, 6, 9, 11
	var d = 0;
	if (isMonth == true) {
		d = parseInt(objday.value, 10);
		if ((m == 4) || (m == 6) || (m == 9) || (m == 11)) {
			if ((d >= 1) && d <= 30) {
				isDay = true;
			}
		} else if ((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m == 10) || (m == 12)) {
			if ((d >= 1) && d <= 31) {
				isDay = true;
			}
		} else {
			if (isYear == true) {
				if ((y % 4) == 0) {
					// ปีที่ mod 4 ลงตัว เดือนกุมภาพันธ์ จะมี 28 วัน
					if ((d >= 1) && d <= 29) {
						isDay = true;
					}
				} else {
					// ปีที่ mod 4 ไม่ลงตัว เดือนกุมภาพันธ์ จะมี 29 วัน
					if ((d >= 1) && d <= 28) {
						isDay = true;
					}
				}
			}
		}
	}

	var picker = dojo.widget.byId(el);
	if (isYear && isMonth && isDay) {
		var mm = (100 + m) + "";
		var dd = (100 + d) + "";
		var tempdate = y + "-" + mm.substring(1,3) + "-" + dd.substring(1,3) + "T00:00:00+07:00";
		picker.setValue(tempdate);
	} else {
		mouseoverpicker = el;
		var d=new Date();
		var syear_today = (parseInt(d.getUTCFullYear().toString())+543).toString();
		var mount_today = parseInt(d.getUTCMonth().toString())+1;
		var day_today = parseInt(d.getDate().toString());
		var sday_today;
		var smount_today;
		if(day_today < 10){
			sday_today = "0"+day_today.toString();
		}
		else{
			sday_today =day_today.toString();
		}
		if(mount_today < 10){
			smount_today = "0"+mount_today.toString();
		}
		else{
			smount_today = mount_today.toString();
		}
		picker.setValue(syear_today+"-"+smount_today+"-"+sday_today+"T00:00:00+07:00");

	}
}
var mouseoverpicker = "";